package com.ruoyi.web.controller.pc.anon;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.blogs.domain.BsComment;
import com.ruoyi.blogs.domain.BsCommentLikeRecord;
import com.ruoyi.blogs.domain.vo.BsCommentVo;
import com.ruoyi.blogs.service.IBsCommentLikeRecordService;
import com.ruoyi.blogs.service.IBsCommentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "评论")
@RestController
@RequestMapping("/anon/comment")
public class AnonCommentController extends BaseController {

    @Autowired
    private IBsCommentService bsCommentService;

    @Autowired
    private IBsCommentLikeRecordService bsCommentLikeRecordService;

    /**
     * 查询评论列表
     */
    @ApiOperation("查询评论列表")
    @GetMapping("/list")
    public TableDataInfo list(BsComment bsComment) {
        startPage();
        try {
            Long userId = SecurityUtils.getUserId();
            bsComment.setUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<BsCommentVo> list = bsCommentService.selectBsNoteCommentList(bsComment);
        return getDataTable(list);
    }

    /**
     * 评论文章或回复用户
     */
    @ApiOperation("评论文章或回复用户")
    @PostMapping
    public R<Integer> comment(@RequestBody BsComment bsComment) {
        if(bsComment.getParentId() == null) {
            bsComment.setParentId(0L);
        }
        return R.ok(bsCommentService.insertBsComment(bsComment));
    }

    /**
     * 评论点赞或点踩
     */
    @ApiOperation("评论点赞或点踩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", dataType = "Long", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "value", value = "0是取消点赞(点踩)，1是点赞(点踩)", dataType = "Integer", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "type", value = "类型（0是点踩，1是点赞）", dataType = "Long", dataTypeClass = Long.class, required = true),
    })
    @Transactional
    @PostMapping("/like")
    public R<String> like(@RequestParam("commentId") Long commentId, Integer value, Long type) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", commentId);
        map.put("value", value);
        map.put("type", type);
        bsCommentService.updateBsCommentLike(map);
        BsComment bsComment = bsCommentService.selectBsCommentById(commentId);
        BsCommentLikeRecord bsCommentLikeRecord = new BsCommentLikeRecord();
        bsCommentLikeRecord.setNoteId(bsComment.getNoteId());
        bsCommentLikeRecord.setOpType(type);
        bsCommentLikeRecord.setCommentId(commentId);
        if(value == 1){
            // 点赞
            bsCommentLikeRecord.setId(IdUtils.fastSimpleUUID());
            bsCommentLikeRecord.setParentId(bsComment.getParentId());
            bsCommentLikeRecordService.insertBsCommentLikeRecord(bsCommentLikeRecord);
        } else {
            // 取消点赞
            bsCommentLikeRecord.setUserId(SecurityUtils.getUserId());
            bsCommentLikeRecordService.deleteBsLikeRecordByQuery(bsCommentLikeRecord);
        }
        return R.ok();
    }

    /**
     * 删除评论
     */
    @ApiOperation("删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论id", dataType = "Long", dataTypeClass = Long.class),
    })
    @Transactional
    @DeleteMapping
    public R<String> remove(@RequestParam("id") Long id) {
        // 删掉评论
        bsCommentService.deleteBsCommentById(id);
        // 删掉一级评论下的回复
        bsCommentService.deleteBsCommentByParentId(id);
        BsCommentLikeRecord bsCommentLikeRecord = new BsCommentLikeRecord();
        bsCommentLikeRecord.setCommentId(id);
        // 删掉这条评论的点踩点赞记录
        bsCommentLikeRecordService.deleteBsLikeRecordByQuery(bsCommentLikeRecord);
        // 删掉这条评论下的回复点踩点赞记录
        bsCommentLikeRecord.setCommentId(null);
        bsCommentLikeRecord.setParentId(id);
        bsCommentLikeRecordService.deleteBsLikeRecordByQuery(bsCommentLikeRecord);
        return R.ok();
    }
}
